package com.hongyun.tms.domain;

import com.hongyun.tms.common.enums.BizCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;
import org.springframework.http.HttpStatus;

/**
 * @author lipeng
 * @date 2024-05-31
 */
@Data
@Builder
@ApiModel("响应信息")
public class AjaxResult<T> {

    private static final int SUCCESS_CODE = HttpStatus.OK.value();
    private static final int COMMON_ERROR_CODE = 1;

    public static final AjaxResult SYSTEM_ERROR_RESULT = AjaxResult.fail(BizCode.SYSTEM_ERROR.getComment());
    private static final AjaxResult NO_DATA_SUCCESS_RESULT = AjaxResult.builder().code(SUCCESS_CODE).build();

    @ApiModelProperty("响应业务码")
    private int code;

    @ApiModelProperty("响应消息")
    private String msg;

    @ApiModelProperty("响应内容")
    private T data;

    public static <T> AjaxResult<T> success(T data) {
        return AjaxResult.<T>builder().code(SUCCESS_CODE).data(data).build();
    }

    public static <T> AjaxResult<T> success() {
        return NO_DATA_SUCCESS_RESULT;
    }

    public static <T> AjaxResult<T> success(String msg, T data) {
        return AjaxResult.<T>builder().code(SUCCESS_CODE).msg(msg).data(data).build();
    }

    public static AjaxResult fail(String errorMsg) {
        return AjaxResult.builder().code(COMMON_ERROR_CODE).msg(errorMsg).build();
    }

    public static AjaxResult fail(int errorCode, String errorMsg) {
        return AjaxResult.builder().code(errorCode).msg(errorMsg).build();
    }

    public static <T> AjaxResult result(int code, String message, T data) {
        return AjaxResult.builder().code(code).msg(message).data(data).build();
    }
}
